package com.dexgdx.game.structure.projectile;

import java.util.ArrayList;
import java.util.List;

import com.dexgdx.game.constants.EObjectId;
import com.dexgdx.game.constants.EParticleType;
import com.dexgdx.game.manager.ParticleManager;
import com.dexgdx.game.structure.base.BaseObject;
import com.dexgdx.game.structure.base.BasePoint;

public class DexTrailObject extends BaseObject{

	private static final long serialVersionUID = 8964321500584743195L;
	
	private List<PooledEffectWrapper> pooledEffectWrappers; 
	private BasePoint trajectory;
	
	public DexTrailObject(BaseObject baseObject, BasePoint trajectory) {
		super(baseObject.x, baseObject.y, baseObject.height, baseObject.width, false, true, EObjectId.DEX_TRAIL.getObjectId());
		this.trajectory = trajectory;
		initialize();
	}
	
	private void initialize(){
		pooledEffectWrappers = new ArrayList<PooledEffectWrapper>();
		x += trajectory.x + width/2;
		y += trajectory.y + height/2;
		PooledEffectWrapper pooledEffectWrapper= new PooledEffectWrapper(ParticleManager.obtainFromDexTrailParticleEffectPool(), EParticleType.TRAIL); 
		pooledEffectWrapper.setPosition(x, y);
		pooledEffectWrappers.add(pooledEffectWrapper);
	}
	
	public void loadNewPosition(){
		x += trajectory.x;
		y += trajectory.y;
		PooledEffectWrapper pooledEffectWrapper = null;
		if(pooledEffectWrappers.size() < 10){
			pooledEffectWrapper = new PooledEffectWrapper(ParticleManager.obtainFromDexTrailParticleEffectPool(), EParticleType.TRAIL);
		}
		else {
			pooledEffectWrapper = pooledEffectWrappers.remove(0);
		}
		pooledEffectWrapper.setPosition(x, y);
		pooledEffectWrappers.add(pooledEffectWrapper);

	}
	
	public boolean areAllParticlesDead(){
		boolean dead = true;
		for (PooledEffectWrapper pooledEffectWrapper : pooledEffectWrappers) {
			if(!pooledEffectWrapper.isComplete()){
				dead = false;
				break;
			}
		}
		return dead;
	}

	public void finalizeObject(){
		pooledEffectWrappers.clear();
	}
	
	public List<PooledEffectWrapper> getPooledEffects() {
		return pooledEffectWrappers;
	}
	
}
